package com.banciyuan.bcywebview.biz.detail.complex.holder;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Complex;

/**
 * Created by squall on 2016/8/15.
 */
public class FloatTop implements View.OnClickListener {

    private LinearLayout viewCounts_ll;
    public TextView focus_tv;
    private TextView viewCounts_tv;
    private TextView name_tv;
    private TextView time_tv;
    private TextView intro_tv;
    private ImageView back_iv;
    private ImageView circle_iv;
    private ImageView vip_iv;
    private LinearLayout back_line;

    private Context mContext;
    private Complex complex;

    private View root;

    public FloatTop(View itemView, int type) {
        this(itemView);
        if (type == 0) {
            back_line.setVisibility(View.GONE);
            itemView.findViewById(R.id.split_line).setVisibility(View.GONE);
        } else {
            back_line.setVisibility(View.VISIBLE);
            itemView.findViewById(R.id.split_line).setVisibility(View.VISIBLE);
        }
    }

    public FloatTop(View itemView) {
        this.root = itemView;
        viewCounts_ll = (LinearLayout) itemView.findViewById(R.id.viewcount_top);
        circle_iv = (ImageView) itemView.findViewById(R.id.civ_header_top);
        vip_iv = (ImageView) itemView.findViewById(R.id.vip_logo_floot);
        back_iv = (ImageView) itemView.findViewById(R.id.iv_back);
        focus_tv = (TextView) itemView.findViewById(R.id.tv_focuse_top);
        viewCounts_tv = (TextView) itemView.findViewById(R.id.tv_viewcount_top);
        name_tv = (TextView) itemView.findViewById(R.id.tv_name_top);
        time_tv = (TextView) itemView.findViewById(R.id.tv_time_top);
        intro_tv = (TextView) itemView.findViewById(R.id.tv_selfintro_top);
        back_line = (LinearLayout) itemView.findViewById(R.id.back_line);

        name_tv.setOnClickListener(this);
        time_tv.setOnClickListener(this);
        intro_tv.setOnClickListener(this);
        circle_iv.setOnClickListener(this);

        back_iv.setOnClickListener(this);
        focus_tv.setOnClickListener(this);
    }

    public void processTopHeadData(Complex complex, Context mContext) {
        this.mContext = mContext;
        this.complex = complex;

        int maxNameWidth = CommonUtils.getWidthPixels((Activity) mContext) - UIUtils.dip2px(228, mContext);
        name_tv.setMaxWidth(maxNameWidth);

        name_tv.setText(complex.getProfile().getUname());
        if (complex.getProfile().isValue_user()) {
            vip_iv.setVisibility(View.VISIBLE);
        } else {
            vip_iv.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(complex.getCtime()))
            time_tv.setText(StringUtil.getOppositeTime(complex.getCtime()));
        if (TextUtils.isEmpty(complex.getProfile().getSelf_intro())) {
            intro_tv.setVisibility(View.GONE);
        } else {
            intro_tv.setVisibility(View.VISIBLE);
            intro_tv.setText(Html.fromHtml(complex.getProfile().getSelf_intro()));
        }
        ImageLoader.getInstance().displayImage(complex.getProfile().getAvatar(), circle_iv, ImageOptions.optionHead);

        if (complex.getProfile().getUid().equals(UserDataHelper.getInstance(mContext).getUid())) {
            viewCounts_ll.setVisibility(View.VISIBLE);
            focus_tv.setVisibility(View.INVISIBLE);
            viewCounts_tv.setText(complex.getView_count());
        } else {
            if (complex.getProfile().getFollowstate().equals("unfollow")) {
                focus_tv.setVisibility(View.VISIBLE);
            } else {
                focus_tv.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.FINISH_EVENT);
                break;
            case R.id.tv_name_top:
            case R.id.tv_selfintro_top:
            case R.id.tv_time_top:
            case R.id.civ_header_top:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.GO_PERSON);
                break;
            case R.id.tv_focuse_top:
                TimelineLikeHelper.doFollow(mContext, "dofollow", complex.getProfile().getUid(), new TimelineLikeHelper.FollowSuccess() {
                    @Override
                    public void onFollowSuccess(String response) {
                        if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                            MyToast.show(mContext, mContext.getString(R.string.focus_succ));
                            BaseObserver.getInstance().notifyWatchers(BaseObserver.FOLLOW_EVENT);
                        }
                    }
                });
                break;
        }
    }

    public View getRoot() {
        return root;
    }
}
